﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        private OrderService _orderService;

        [TestInitialize]
        public void SetUp()
        {
            _orderService = new OrderService();
        }

        [TestMethod]
        public void TestAddOrder()
        {
            // Arrange
            Order order1 = new Order(1, "Alice");

            // Act
            _orderService.AddOrder(order1);

            // Assert
            Assert.AreEqual(1, _orderService.Orders.Count);
            Assert.IsTrue(_orderService.Orders.Contains(order1));
        }

        [TestMethod]
        public void TestDeleteOrder()
        {
            // Arrange
            Order order1 = new Order(1, "Alice");
            _orderService.AddOrder(order1);

            // Act
            _orderService.DeleteOrder(1);

            // Assert
            Assert.AreEqual(0, _orderService.Orders.Count);
            Assert.IsFalse(_orderService.Orders.Contains(order1));
        }

        [TestMethod]
        public void TestUpdateOrder()
        {
            // Arrange
            Order order1 = new Order(1, "Alice");
            _orderService.AddOrder(order1);

            Order updatedOrder = new Order(1, "John");

            // Act
            _orderService.UpdateOrder(updatedOrder);

            // Assert
            Order updated = _orderService.Orders.First(o => o.Id == 1);
            Assert.AreEqual("John", updated.Customer);
        }

        [TestMethod]
        public void TestQueryOrdersByOrderNumber()
        {
            // Arrange
            Order order1 = new Order(1, "Alice");
            Order order2 = new Order(2, "Bob");
            _orderService.AddOrder(order1);
            _orderService.AddOrder(order2);

            // Act
            var result = _orderService.QueryOrdersByOrderNumber(1);

            // Assert
            Assert.AreEqual(1, result.Count());
            Assert.IsTrue(result.Any(o => o.Id == 1));
        }

        [TestMethod]
        public void TestQueryOrdersByProductName()
        {
            // Arrange
            Order order1 = new Order(1, "Alice");
            Order order2 = new Order(2, "Bob");
            order1.OrderDetails.Add(new OrderDetail(1, "Product1", 3, 10));
            order2.OrderDetails.Add(new OrderDetail(1, "Product1", 1, 10));
            _orderService.AddOrder(order1);
            _orderService.AddOrder(order2);

            // Act
            var result = _orderService.QueryOrdersByProductName("Product1");

            // Assert
            Assert.AreEqual(2, result.Count());
            Assert.IsTrue(result.Any(o => o.Id == 1));
            Assert.IsTrue(result.Any(o => o.Id == 2));
        }

        [TestMethod]
        public void TestQueryOrdersByCustomer()
        {
            // Arrange
            Order order1 = new Order(1, "Alice");
            Order order2 = new Order(2, "Bob");
            _orderService.AddOrder(order1);
            _orderService.AddOrder(order2);

            // Act
            var result = _orderService.QueryOrdersByCustomer("Alice");

            // Assert
            Assert.AreEqual(1, result.Count());
            Assert.IsTrue(result.Any(o => o.Id == 1));
        }

        [TestMethod]
        public void TestQueryOrdersByAmount()
        {
            // Arrange
            Order order1 = new Order(1, "Alice");
            Order order2 = new Order(2, "Bob");
            order1.OrderDetails.Add(new OrderDetail(1, "Product1", 3, 10));
            order2.OrderDetails.Add(new OrderDetail(1, "Product1", 1, 10));
            _orderService.AddOrder(order1);
            _orderService.AddOrder(order2);

            // Act
            var result = _orderService.QueryOrdersByAmount(30);

            // Assert
            Assert.AreEqual(1, result.Count());
            Assert.IsTrue(result.Any(o => o.Id == 1));
        }

        [TestMethod]
        public void TestSortOrders()
        {
            // Arrange
            Order order1 = new Order(3, "Alice");
            Order order2 = new Order(1, "Bob");
            Order order3 = new Order(2, "Charlie");
            _orderService.AddOrder(order1);
            _orderService.AddOrder(order2);
            _orderService.AddOrder(order3);

            // Act
            _orderService.SortOrders();

            // Assert
            Assert.AreEqual(3, _orderService.Orders.Count);
            Assert.AreEqual(1, _orderService.Orders[0].Id);
            Assert.AreEqual(2, _orderService.Orders[1].Id);
            Assert.AreEqual(3, _orderService.Orders[2].Id);
        }


        [TestMethod]
        public void TestExportAndImport()
        {
            // Arrange
            Order order1 = new Order(1, "Alice");
            _orderService.AddOrder(order1);

            // Act
            _orderService.Export("test_orders.xml");
            _orderService.DeleteOrder(1);
            _orderService.Import("test_orders.xml");

            // Assert
            Assert.AreEqual(1, _orderService.Orders.Count);
            Assert.IsTrue(_orderService.Orders.Any(o => o.Id == 1 && o.Customer == "Alice"));
        }
    }
}
